package com.cardosystems.tests;

import java.io.File;
import java.util.List;

import jsystem.extensions.analyzers.text.FindText;
import jsystem.framework.TestProperties;
import jsystem.framework.analyzer.AnalyzerException;
import jsystem.utils.FileUtils;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.python.modules.re;

import systemobject.terminal.RS232;

import com.cardosystems.infra.CardoWindowsStation;
import com.cardosystems.infra.GmailClient;
import com.cardosystems.infra.enums.Gender;
import com.cardosystems.infra.enums.PSKeys;
import com.cardosystems.infra.enums.ProductTypes;
import com.cardosystems.infra.enums.ServerType;
import com.cardosystems.infra.pages.CardoCommunityEditProfilePage;
import com.cardosystems.infra.pages.CardoCommunityHomePage;
import com.cardosystems.infra.pages.CardoCommunitySettingsPage;
import com.cardosystems.infra.pages.ProductRegistrationPage;
import com.cardosystems.infra.pages.UserRegistrationPage;
import com.cardosystems.tests.analyzers.PSKeyAnalyzer;
import com.googlecode.gmail4j.GmailMessage;

public class CardoPocCodeBased extends AbstractWebDriverTest {

	private GmailClient client;
	private CardoWindowsStation station;
	private boolean rdsState;
	private String email;
	private String password;
	private String confPassword;
	private String confEmail;
	private String firstName;
	private String lastName;
	private String buddyName;
	private Gender gender;
	private String year;
	private String country;
	private ProductTypes productType;
	private String date;
	private String storeName;

	boolean newCardoProduct;
	boolean cardoCommunityFanClub;
	private String expectedWord;

	private ServerType serverType;

	@Before
	public void initClass() throws Exception {
		client = (GmailClient) system.getSystemObject("gmailClient");
		station = (CardoWindowsStation) system.getSystemObject("station");
	}

	public void getUnreadMessages() throws Exception {
		List<GmailMessage> list = client.getUnreadMesseges();
		for (GmailMessage msg : list) {
			System.out.println(msg);
		}

	}

	@Test
	@TestProperties(name = "Register New User", paramsInclude = { "serverType,email,confEmail,firstName,lastName,password,confPassword,gender,year,country,productType,date,storeName,newCardoProduct,cardoCommunityFanClub" })
	public void registerNewUser() throws Exception {
		// closing Cardo updater
		report.report("Closing Cardo Updater");
		station.closeCardoUpdater();
		// navigate to login page / user registartion
		report.report("Navigate to Login Page");
		UserRegistrationPage registrationPage = new UserRegistrationPage(
				driver, serverType);
		// fill in new user detail , click save and move to product registration
		// page
		report.report("Filling New User Details");
		ProductRegistrationPage productRegistrationPage = registrationPage
				.registerNewUser(email, confEmail, firstName, lastName,
						password, confPassword);
		// fill in product details and move to Cardo community home page
		report.report("Filling Product Details");
		CardoCommunityHomePage communityHomePage = productRegistrationPage
				.setProductProperties(gender, year, country, productType, date,
						storeName, newCardoProduct, cardoCommunityFanClub);
		// logout from Cardo community
		report.report("Logging Out from Cardo Community");
		communityHomePage.logout();
		// open Cardo updater
		report.report("Open Cardo Updater");
		station.openCardoUpdater();
	}

	@Test
	@TestProperties(name = "Register New User with Errors", paramsInclude = { "serverType,email,confEmail,firstName,lastName,password,confPassword,gender,year,country,productType,date,storeName,newCardoProduct,cardoCommunityFanClub" })
	public void registerUnsuccessfulUser() throws Exception {
		// closing Cardo updater
		report.report("Closing Cardo Updater");
		station.closeCardoUpdater();
		// navigate to login page / user registartion
		report.report("Navigate to Login Page");
		UserRegistrationPage registrationPage = new UserRegistrationPage(
				driver, serverType);
		// enter new user details with some errors
		boolean result = registrationPage.reigsterNewUserWithWrongDetails(
				confEmail, confEmail, firstName, lastName, password,
				confPassword);
		// open Cardo updater
		report.report("Open Cardo Updater");
		station.openCardoUpdater();
		if (result == false) {
			report.report("No Errors were Found not as Expected", result);
		} else {
			report.report("Errors were Found as Expected");
		}
	}

	@Test
	@TestProperties(name = "Register Device with ${buddyName} and Validate", paramsInclude = { "serverType,email,password,buddyName" })
	public void signIn() throws Exception {
		// navigate to login page / user registartion
		report.report("Navigate to Login Page");
		UserRegistrationPage registrationPage = new UserRegistrationPage(
				driver, serverType);
		// log in as register user
		report.report("Loggin In to Cardo Community Home Page as Register User");
		CardoCommunityHomePage homePage = registrationPage.signIn(email,
				password);
		// navigate to edit profile page
		report.report("Navigate to Edit Profile Page");
		CardoCommunityEditProfilePage editProfilePage = homePage
				.clickEditProfile();
		// fill device info, in this case only "Buddy name"
		report.report("Filling Device's Name");
		editProfilePage.fillDeviceDetails(buddyName);
		report.report("Syncing");

		boolean isSaved = editProfilePage.saveDeviceDeatils();
		if (!isSaved) {
			report.report("Cannot Save the Buddy Name", isSaved);
		} else {
			report.report("Device Name was Saved as " + buddyName);
		}
		// log out from Cardo community
		report.report("Logging Out from Cardo Community");
		editProfilePage.logout();
		// closing Cardo updater
		report.report("Closing Cardo Updater");
		station.closeCardoUpdater();
		// disable USB device
		report.report("Disabling USB device");
		station.disableUSBDevice();
		sleep(15000);
		// enable USB device
		report.report("Enabling USB device");
		station.enableUSBDevice();
		// run PsKey35 wait until it's done and close it
		report.report("Start Retrieving Rider's Dump File from Headset");
		station.runPsKey35();
		// get riders dump files
		station.getRiderDumpFilesFromUUT();
		String name = FileUtils.read(new File("Riders.nam"));
		report.report(name);
		// open Cardo updater
		report.report("Open Cardo Updater");
		station.openCardoUpdater();

	}

	@Test
	@TestProperties(name = "Switch RDS to ${rdsState} and Validate RDS State", paramsInclude = {
			"serverType,rdsState", "email", "password", "expectedWord" })
	public void validatePskeyUser2() throws Exception {
		// navigate to login page / user registartion
		report.report("Navigate to Login Page");
		UserRegistrationPage registrationPage = new UserRegistrationPage(
				driver, serverType);
		// log in as register user
		report.report("Loggin In to Cardo Community Home Page as Register User");
		CardoCommunityHomePage homePage = registrationPage.signIn(email,
				password);
		// navigate to setting page
		report.report("Navigate to Setting Page");
		CardoCommunitySettingsPage settings = homePage.navigateToSettingPage();
		// try to change RDS status if status already as requested the test will
		// fail
		report.report("Change RDS Status to " + rdsState);
		boolean isRdsStatusChanged = settings.switchRdsState(rdsState);
		//if RDS status was not changed
		if (!isRdsStatusChanged){
			settings.logout();
			report.report("RDS was already "+rdsState,isRdsStatusChanged);
			throw new AnalyzerException();
		}
		
		// sync the change
		report.report("Syncing");
		homePage.ClickOnSyncBtn();
		report.report("Syncing Done");
		// log out from Cardo community
		report.report("Logging Out from Cardo Community");
		homePage.logout();
		// closing Cardo updater
		report.report("Closing Cardo Updater");
		station.closeCardoUpdater();
		// querying PSKeys
		report.report("Querying PS Keys");
		String result = station.queryPsKeys();
		// open Cardo updater
		report.report("Open Cardo Updater");
		station.openCardoUpdater();
		PSKeyAnalyzer psKeyAnalyzer = new PSKeyAnalyzer(PSKeys.PSKEY_USR2, 1,
				expectedWord);
		analyzer.setTestAgainstObject(result);
		analyzer.analyze(psKeyAnalyzer);

	}

	public boolean isRdsState() {
		return rdsState;
	}

	public void setRdsState(boolean rdsState) {
		this.rdsState = rdsState;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfPassword() {
		return confPassword;
	}

	public void setConfPassword(String confPassword) {
		this.confPassword = confPassword;
	}

	public String getConfEmail() {
		return confEmail;
	}

	public void setConfEmail(String confEmail) {
		this.confEmail = confEmail;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getBuddyName() {
		return buddyName;
	}

	public void setBuddyName(String buddyName) {
		this.buddyName = buddyName;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public ProductTypes getProductType() {
		return productType;
	}

	public void setProductType(ProductTypes productType) {
		this.productType = productType;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getStoreName() {
		return storeName;
	}

	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}

	public boolean isNewCardoProduct() {
		return newCardoProduct;
	}

	public void setNewCardoProduct(boolean newCardoProduct) {
		this.newCardoProduct = newCardoProduct;
	}

	public boolean isCardoCommunityFanClub() {
		return cardoCommunityFanClub;
	}

	public void setCardoCommunityFanClub(boolean cardoCommunityFanClub) {
		this.cardoCommunityFanClub = cardoCommunityFanClub;
	}

	public String getExpectedWord() {
		return expectedWord;
	}

	public void setExpectedWord(String expectedWord) {
		this.expectedWord = expectedWord;
	}

	public ServerType getServerType() {
		return serverType;
	}

	public void setServerType(ServerType serverType) {
		this.serverType = serverType;
	}

}
